package com.xunxi.mimi.ui.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.xunxi.mimi.utils.ListUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:简单封装recycleview的adapter
 * liyu
 * 注：如果用到viewType请重写getAdvanceViewType
 */

public abstract class BaseRecycleAdapter<T> extends RecyclerView.Adapter<RecycleViewHolder> {

    private static final int VIEW_TYPE_HEADER = -10000;
    private static final int VIEW_TYPE_FOOTER = 10000;
    private ArrayList<View> mHeaderViews = new ArrayList<>(); //头视图
    private ArrayList<View> mFooterViews = new ArrayList<>();   //尾视图

    private ArrayList<Integer> mHeaderViewTypes = new ArrayList<>();
    private ArrayList<Integer> mFooterViewTypes = new ArrayList<>();
    private RecyclerView.LayoutManager manager;

    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List<T> mDatas = new ArrayList<>();

    public BaseRecycleAdapter(Context context) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
    }

    public BaseRecycleAdapter(Context context, List<T> mDatas) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mDatas = mDatas;
    }

    public BaseRecycleAdapter(Context context, List<T> mDatas, RecyclerView.LayoutManager manager) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mDatas = mDatas;
        this.manager = manager;
    }

    /**
     * 设置适配器的数据
     *
     * @param dataList
     */
    public void setDataSource(List<T> dataList) {
        mDatas.clear();

        if (dataList != null) {
            mDatas.addAll(dataList);
        }
        notifyDataSetChanged();
    }

    public ArrayList<T> getDatas() {
        return (ArrayList<T>) mDatas;
    }

    public Context getContext() {
        return mContext;
    }

    /**
     * 添加头视图
     *
     * @param headerView
     */
    public void addHeaderView(View headerView) {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        headerView.setLayoutParams(params);
        mHeaderViews.add(headerView);
    }

    /**
     * 清除头部view
     */
    public void removeHeaderView() {
        mHeaderViews.clear();
        mHeaderViewTypes.clear();
    }


    /**
     * 添加脚视图
     *
     * @param footerView 尾视图
     */
    public void addFooterView(View footerView) {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        footerView.setLayoutParams(params);
        mFooterViews.add(footerView);
        notifyDataSetChanged();
    }

    /**
     * 清除尾部view
     */
    public void removeFooterView() {
        mFooterViews.clear();
        mFooterViewTypes.clear();
//        notifyDataSetChanged();
    }

    /**
     * 禁止重写，如果有header跟footer
     * type从1开始
     */
    @Override
    public int getItemViewType(int position) {

        if (mHeaderViews.size() > 0 && position < mHeaderViews.size()) {
            //用position作为HeaderView 的   ViewType标记
            //记录每个ViewType标记
            mHeaderViewTypes.add(position * VIEW_TYPE_HEADER);
            return position * VIEW_TYPE_HEADER;
        }

        if (mFooterViews.size() > 0 && position > getAdvanceCount() - 1 + mHeaderViews.size()) {
            //用position作为FooterView 的   ViewType标记
            //记录每个ViewType标记
            mFooterViewTypes.add(position * VIEW_TYPE_FOOTER);
            return position * VIEW_TYPE_FOOTER;
        }

        if (mHeaderViews.size() > 0) {
            return getAdvanceViewType(position - mHeaderViews.size());
        }
        return getAdvanceViewType(position);
    }

    /**
     * Item页布局类型个数，默认为1
     * 重写这个，切勿重写getItemViewType
     *
     * @param position
     */
    public int getAdvanceViewType(int position) {
        return 1;
    }

    /**
     * Item的真实总数
     *
     * @return
     */
    public int getAdvanceCount() {
        if (mDatas != null) {
            return mDatas.size();
        }
        return 0;
    }

    private void onBindAdvanceViewHolder(RecycleViewHolder holder, int position) {
        getItemView(holder, position, mDatas.get(position));
    }

    /**
     * 设置每个页面显示的内容
     *
     * @param holder itemHolder
     * @param item   每一Item显示的数据
     */
    public abstract void getItemView(RecycleViewHolder holder, int position, T item);

    /**
     * 创建ViewHolder
     *
     * @param parent   RecycleView对象
     * @param viewType viee类型
     * @return Holder对象
     */
    protected RecycleViewHolder onCreateAdvanceViewHolder(ViewGroup parent, int viewType) {
        View v = mInflater.inflate(getItemResource(viewType), parent, false);
        return new RecycleViewHolder(v, this);
    }

    public abstract int getItemResource(int viewType);

    @Override
    public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderViewTypes.contains(viewType)) {
            return new HeaderHolder(mHeaderViews.get(viewType / VIEW_TYPE_FOOTER));
        }

        if (mFooterViewTypes.contains(viewType)) {
            int index = viewType / VIEW_TYPE_FOOTER - getAdvanceCount() - mHeaderViews.size();
            return new FooterHolder(mFooterViews.get(index));
        }
        return onCreateAdvanceViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecycleViewHolder holder, int position) {
        if (mFooterViews.size() > 0 && (position > getAdvanceCount() - 1 + mHeaderViews.size())) {
            return;
        }

        if (mHeaderViews.size() > 0) {
            if (position < mHeaderViews.size()) {
                return;
            }
            onBindAdvanceViewHolder(holder, position - mHeaderViews.size());
            return;
        }
        onBindAdvanceViewHolder(holder, position);
    }

    class HeaderHolder extends RecycleViewHolder {

        public HeaderHolder(View itemView) {
            super(itemView);
        }
    }

    class FooterHolder extends RecycleViewHolder {

        public FooterHolder(View itemView) {
            super(itemView);
        }
    }

    public T getItem(int position) {
        return ListUtil.isEmpty(mDatas) ? null : mDatas.get(position);
    }

    @Override
    public int getItemCount() {
        if (mHeaderViews.size() > 0 && mFooterViews.size() > 0) {
            return getAdvanceCount() + mHeaderViews.size() + mFooterViews.size();
        }
        if (mHeaderViews.size() > 0) {
            return getAdvanceCount() + mHeaderViews.size();
        }
        if (mFooterViews.size() > 0) {
            return getAdvanceCount() + mFooterViews.size();
        }
        return getAdvanceCount();
    }

    //设置数据
    public void setItems(List<T> mDatas) {
        if (!ListUtil.isEmpty(mDatas)) {
            this.mDatas = mDatas;
            notifyDataSetChanged();
        }
    }

    //添加数据
    public void addItems(List<T> mDatas) {
        if (!ListUtil.isEmpty(mDatas)) {
            this.mDatas.addAll(mDatas);
            notifyItemRangeInserted(getAdvanceCount() + getHeaderViewsCount() - mDatas.size(), mDatas.size());
//            notifyDataSetChanged();
        }
    }

    public void addItem(T item) {
        if (item != null) {
            mDatas.add(item);
            notifyItemInserted(getItemCount() - 1);
        }
    }

    /**
     * 刷新某个位置view，除去headview数目
     *
     * @param position
     */
    public void notifyRecyeItemChanged(int position) {
        notifyItemChanged(position + getHeaderViewsCount());
    }

    /**
     * 刷新某个位置view，除去headview数目
     *
     * @param position
     */
    public void notifyRecyeItemRemoved(int position) {
        notifyItemRemoved(position + getHeaderViewsCount());
    }

    public void addItem(T item, int position) {
        if (item != null) {
            mDatas.add(position, item);
            notifyItemInserted(position + getHeaderViewsCount());
        }
    }

    public void removeItem(int position) {
        if (position >= 0) {
            mDatas.remove(position);
            notifyRecyeItemRemoved(position);
        }
    }

//    //添加数据
//    public void addInsertItems(List<T> mDatas) {
//        if(!CollectionUtil.isEmpty(mDatas)) {
//            this.mDatas.addAll(mDatas);
//            notifyItemInserted(mDatas.size() - 1);
////            notifyDataSetChanged();
//        }
//
//    }

    //添加数据
    public void cleanItems() {
        mDatas.clear();
        notifyDataSetChanged();
    }

    public int getHeaderViewsCount() {
        return mHeaderViews.size();
    }

    public int getFooterViewsCount() {
        return mFooterViews.size();
    }


    protected OnItemClickListener listener;


    public OnItemClickListener getItemClickListener() {
        return listener;
    }

    /**
     * 设置item点击事件监听器
     *
     * @param listener 监听器对象
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * item点击事件回调
     */
    public interface OnItemClickListener {
        void onItemClickListener(View view, int position);
    }

    protected OnItemLongClickListener mLongClicklistener;


    public OnItemLongClickListener getItemLongClickListener() {
        return mLongClicklistener;
    }

    /**
     * 设置item点击事件监听器
     *
     * @param listener 监听器对象
     */
    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.mLongClicklistener = listener;
    }

    /**
     * item长按事件回调
     */
    public interface OnItemLongClickListener {
        void onItemLongClickListener(RecyclerView.ViewHolder viewHolder, int position);
    }
}
